package com.webchat.user.service.relation;


import com.webchat.common.enums.AccountRelationTypeEnum;
import com.webchat.common.enums.RoleCodeEnum;
import com.webchat.common.exception.BusinessException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AccountRelationFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private static Map<Integer, AbstractAccountRelationService> services = new HashMap<>();

    private static Map<Integer, AbstractAccountRelationService> serviceForType = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        this.registryServiceByRole();
        this.registryServiceByRelationType();
    }

    /**
     * 根据账号角色注册服务bean
     *
     */
    private void registryServiceByRole() {

        services.put(RoleCodeEnum.USER.getCode(), applicationContext.getBean(User2UserAccountRelationService.class));
        services.put(RoleCodeEnum.ADMIN.getCode(), applicationContext.getBean(User2UserAccountRelationService.class));
        services.put(RoleCodeEnum.ROBOT.getCode(), applicationContext.getBean(User2RobotAccountRelationService.class));
        services.put(RoleCodeEnum.GROUP.getCode(), applicationContext.getBean(User2GroupAccountRelationService.class));
        services.put(RoleCodeEnum.PUBLIC_ACCOUNT.getCode(), applicationContext.getBean(User2OfficialAccountRelationService.class));
        services.put(RoleCodeEnum.AIBOT.getCode(), applicationContext.getBean(User2AIAgentSenderAccountRelationService.class));
        services.put(RoleCodeEnum.SERVER_ACCOUNT.getCode(), applicationContext.getBean(User2ServerAccountRelationService.class));
    }

    /**
     * 根据关系类型注册服务bean
     */
    private void registryServiceByRelationType() {

        serviceForType.put(AccountRelationTypeEnum.USER_USER.getType(),
                applicationContext.getBean(User2UserAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_GROUP.getType(),
                applicationContext.getBean(User2GroupAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_OFFICIAL.getType(),
                applicationContext.getBean(User2OfficialAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_ROBOT.getType(),
                applicationContext.getBean(User2RobotAccountRelationService.class));
        serviceForType.put(AccountRelationTypeEnum.USER_SERVER.getType(),
                applicationContext.getBean(User2ServerAccountRelationService.class));
    }

    public static AbstractAccountRelationService getServiceByCode(Integer roleCode) {
        AbstractAccountRelationService accountRelationService = services.get(roleCode);
        if (accountRelationService != null) {
            return accountRelationService;
        }
        throw new BusinessException("不支持的关系服务");
    }

    public static AbstractAccountRelationService getServiceByType(Integer relationType) {
        AbstractAccountRelationService accountRelationService = serviceForType.get(relationType);
        if (accountRelationService != null) {
            return accountRelationService;
        }
        throw new BusinessException("不支持的关系服务");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
